package Eyjafjalla.action;

import Eyjafjalla.helper.StringHelper;
import basemod.cardmods.EtherealMod;
import basemod.helpers.CardModifierManager;
import com.badlogic.gdx.graphics.Color;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.Iterator;

public class MissSoundAction extends AbstractGameAction {
    public MissSoundAction(boolean isFire){
        actionType = ActionType.CARD_MANIPULATION;
        startDuration = duration = Settings.ACTION_DUR_FAST;
        this.isFire = isFire;
    }

    boolean isFire;

    private static final UIStrings uiStrings;

    @Override
    public void update() {
        if(startDuration==duration){
            AbstractPlayer p = AbstractDungeon.player;
            if(p.hand.size()==0){
                this.isDone = true;
                return;
            }
            else if(p.hand.size()==1){
                AbstractCard c = p.hand.getTopCard();
                if(c.cost>=0){
                    c.updateCost(-c.cost);
                    c.flash();
                }
                this.isDone = true;
                return;
            }
            else {
                AbstractDungeon.handCardSelectScreen.open(uiStrings.TEXT[1],1,false,false);
            }
        }
        else if(!AbstractDungeon.handCardSelectScreen.wereCardsRetrieved) {
            Iterator<AbstractCard> var2 = AbstractDungeon.player.hand.group.iterator();
            while(var2.hasNext()){
                AbstractCard c = var2.next();
                if(c.cost>=0){
                    c.updateCost(1);
                    c.flash(Color.RED.cpy());
                    if(isFire)
                        CardModifierManager.addModifier(c, new EtherealMod());
                }
            }
            Iterator<AbstractCard> var1 = AbstractDungeon.handCardSelectScreen.selectedCards.group.iterator();
            while (var1.hasNext()){
                AbstractCard c = var1.next();
                if(c.cost>=0){
                    c.updateCost(-c.cost);
                    c.flash(Color.GOLD.cpy());
                    AbstractDungeon.player.hand.addToTop(c);
                }
            }
            AbstractDungeon.handCardSelectScreen.wereCardsRetrieved = true;
        }

        tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
    }
}
